﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;


namespace GSLMS.WebAPP.Areas.Contract.Controllers.ConAgr
{
    public class ConAgrController : AdminBaseController
    {
        protected override string navTabId
        { get { return "Contract_ConAgr"; } }


        public ActionResult Index(FormCollection form)
        {
            var searchParma = new TB_LOAN_CONTRACT();

            searchParma = base.SearchPara<TB_LOAN_CONTRACT>(form, searchParma);

            searchParma.BUSSTYPE = string.IsNullOrEmpty(Request["BUSSTYPE"]) ? -1 : Helper.ToInt(Request["BUSSTYPE"], -1);
            searchParma.STATUS = string.IsNullOrEmpty(Request["STATUS"]) ? 0 : Helper.ToInt(Request["STATUS"], 0);
            searchParma.STATUS2 = string.IsNullOrEmpty(Request["STATUS2"]) ? 0 : Helper.ToInt(Request["STATUS2"], 0);
            searchParma.SYSTEMID = string.IsNullOrEmpty(Request["SYSTEMID"]) ? -1 : Helper.ToInt(Request["SYSTEMID"], -1);
            searchParma.StrSort = " id desc";

            ViewData["systemIDList"] = EnumHelper.ToSelectList<ApplyEnum.RegSystemID>();
            ViewData["bussTypeList"] = EnumHelper.ToSelectList<ApplyEnum.RegBussType>();

            var list = serviceContract.GetList(searchParma);
            ViewData["RowCount"] = searchParma.RowCount;
            return View(list);
        }

        #region 查看
        public ActionResult Vcontract(long id)
        {
           
            return View();
        }
        #endregion

        #region 生成
        public ActionResult AccountType()
        {
            

            return View();
        }

        [HttpGet]
        public ActionResult Create(long id)
        {
            var model = serviceProInfo.GetByID(id);
            var conModel = new GSLMS.Entity.TB_LOAN_CONTRACT();

            ViewData["ProComInfo"] = serviceProComInfo.GetConfirmProComInfo(id);
            conModel = serviceLoanContract.GetByProID(id);
            ViewData["contractInfo"] = conModel;
            if(conModel==null||conModel.TOUSER==null||conModel.TOUSER.Trim()=="")
              ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname");
            else
                ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "empname",conModel.TOUSER);
            
            return View(model);
        }


        [HttpPost]
        public string Create(FormCollection collection)
        {
            string result = string.Empty;

            TB_LOAN_CONTRACT model = new TB_LOAN_CONTRACT();

            var proModel = new TB_APP_PROINFO();
            TryUpdateModel(proModel, collection);

            if (proModel != null && proModel.contractAccList != null)
                model.contractAccList = proModel.contractAccList;


            try
            {
                model.CONTRACTNO = Request["contractNo"];
                model.STARTDATE = Convert.ToDateTime(Request["StartDate"].Trim());
                model.ENDDATE = Convert.ToDateTime(Request["Endate"].Trim());
                model.PROID = Helper.ToLong(Request["id"]);
                model.CREATEBY = CurentUser.UserName;
                model.CREATEDATE = DateTime.Now;
                model.REMARK = Request["AuditContent"].Trim();
                model.TOUSER = Request["toUser"].Trim();
                model.STATUS = Helper.ToInt(Request["hidActiondo"].Trim());

                bool isSumbit = false;
                isSumbit = serviceContract.Update(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                {
                    result = AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        #endregion

        #region 审批
        [HttpGet]
        public ActionResult Audit(long id)
        {


            return View();
        }


        [HttpPost]
        public string Audit(FormCollection collection)
        {
            string result = string.Empty;

            //TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
            //TryUpdateModel(model, collection);

            //try
            //{
            //    model.CREATEBY = CurentUser.UserName;
            //    model.CREATEDATE = DateTime.Now;

            //    bool isSumbit = false;
            //    isSumbit = serviceContractTmp.Add(model);
            //    if (isSumbit)
            //    { result = AjaxResultSucess; }
            //    else
            //    {
            //        result = AjaxResultFail;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    base.ExceptionHand(ex);
            //    result = AjaxResultFail;
            //}

            return "{" + result + "}";
        }

        #endregion

        #region 打印
        [HttpGet]
        public ActionResult PrnContract(long id)
        {


            return View();
        }


        [HttpPost]
        public string PrnContract(FormCollection collection)
        {
            string result = string.Empty;

            //TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
            //TryUpdateModel(model, collection);

            //try
            //{
            //    model.CREATEBY = CurentUser.UserName;
            //    model.CREATEDATE = DateTime.Now;

            //    bool isSumbit = false;
            //    isSumbit = serviceContractTmp.Add(model);
            //    if (isSumbit)
            //    { result = AjaxResultSucess; }
            //    else
            //    {
            //        result = AjaxResultFail;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    base.ExceptionHand(ex);
            //    result = AjaxResultFail;
            //}

            return "{" + result + "}";
        }

        #endregion

        #region 确认签订
        [HttpGet]
        public ActionResult SignContract(long id)
        {


            return View();
        }


        [HttpPost]
        public string SignContract(FormCollection collection)
        {
            string result = string.Empty;

            //TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
            //TryUpdateModel(model, collection);

            //try
            //{
            //    model.CREATEBY = CurentUser.UserName;
            //    model.CREATEDATE = DateTime.Now;

            //    bool isSumbit = false;
            //    isSumbit = serviceContractTmp.Add(model);
            //    if (isSumbit)
            //    { result = AjaxResultSucess; }
            //    else
            //    {
            //        result = AjaxResultFail;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    base.ExceptionHand(ex);
            //    result = AjaxResultFail;
            //}

            return "{" + result + "}";
        }

        #endregion

        #region 开当票
        [HttpGet]
        public ActionResult TicContract(long id)
        {


            return View();
        }


        [HttpPost]
        public string TicContract(FormCollection collection)
        {
            string result = string.Empty;

            //TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
            //TryUpdateModel(model, collection);

            //try
            //{
            //    model.CREATEBY = CurentUser.UserName;
            //    model.CREATEDATE = DateTime.Now;

            //    bool isSumbit = false;
            //    isSumbit = serviceContractTmp.Add(model);
            //    if (isSumbit)
            //    { result = AjaxResultSucess; }
            //    else
            //    {
            //        result = AjaxResultFail;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    base.ExceptionHand(ex);
            //    result = AjaxResultFail;
            //}

            return "{" + result + "}";
        }

        #endregion

    }
}
